package com.aaa.controller.goods;

import com.aaa.entity.Category;
import com.aaa.service.CategoryService;
import com.aaa.service.impl.CategoryServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddCategoryServlet")
public class AddCategoryServlet extends HttpServlet {
    CategoryService categoryService = null;
    @Override
    public void init() throws ServletException {
        categoryService = new CategoryServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String status = req.getParameter("status");
        String momo = req.getParameter("momo");

        Category category = new Category();
        category.setName(name);
        category.setStatus(Integer.valueOf(status));
        category.setMomo(momo);

        int i = categoryService.addCategory(category);
        resp.getWriter().print(new Gson().toJson(category));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
